package com.learn.seven;

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/15
 */
public class TestMutilLock {

    public static void main(String[] args) {
        BigRoom bigRoom = new BigRoom();

        new Thread(()->{
            bigRoom.study();
        },"小南").start();

        new Thread(()->{
            bigRoom.sleep();
        },"小女").start();
    }
}

class BigRoom{
    private static final Logger log=Logger.getLogger(BigRoom.class);

//    两把锁
    private final Object studyRoom=new Object();

    private final Object bedRoom=new Object();

    public void sleep(){
        try {
            synchronized (bedRoom){
                log.debug("sleeping 2小时");
                TimeUnit.SECONDS.sleep(2);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void study(){
        try {
            synchronized (studyRoom){
                log.debug("study 1小时");
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
